<?php

/**
 * Description of CreatedUpdatedAt
 *
 * @author Gigi Pastore (gigi@la-fabbrica.org)
 */

namespace Registro\Models\Helpers;

trait CreatedUpdatedAt {

    public function beforeValidationOnCreate()
    {
        // Find the {table}_created column, and set it at Db NOW()
        foreach( array_keys( $this->toArray() ) as $key ){
            if( substr( $key, -7 ) == 'created' )
            {
                $this->$key = new \Phalcon\Db\RawValue( 'now()' );
            }
        }
    }

    public function beforeValidationOnUpdate()
    {
        // Find the {table}_modified column, and set it at Db NOW()
        foreach( array_keys( $this->toArray() ) as $key ){
            if( substr( $key, -8 ) == 'modified' )
            {
                $this->$key = new \Phalcon\Db\RawValue( 'now()' );
            }
        }
    }

}
